package com.ruoyi.whzj.controller;

import java.util.List;
import java.util.Random;

import com.ruoyi.whzj.domain.WhActivityUser;
import com.ruoyi.whzj.domain.WhIntegralHistory;
import com.ruoyi.whzj.domain.WhUser;
import com.ruoyi.whzj.service.IWhActivityUserService;
import com.ruoyi.whzj.service.IWhIntegralHistoryService;
import com.ruoyi.whzj.service.IWhUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.whzj.domain.WhActivity;
import com.ruoyi.whzj.service.IWhActivityService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author ruoyi
 * @date 2020-10-22
 */
@Controller
@RequestMapping("/whzj/activity")
public class WhActivityController extends BaseController
{
    private String prefix = "whzj/activity";

    @Autowired
    private IWhActivityService whActivityService;
    @Autowired
    private IWhActivityUserService whActivityUserService;
    @Autowired
    private IWhIntegralHistoryService whIntegralHistoryService;
    @Autowired
    private IWhUserService whUserService;
    @RequiresPermissions("whzj:activity:view")
    @GetMapping()
    public String activity()
    {
        return prefix + "/activity";
    }

    /**
     * 查询活动列表
     */
    @RequiresPermissions("whzj:activity:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WhActivity whActivity)
    {
        startPage();
        List<WhActivity> list = whActivityService.selectWhActivityList(whActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @RequiresPermissions("whzj:activity:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WhActivity whActivity)
    {
        List<WhActivity> list = whActivityService.selectWhActivityList(whActivity);
        ExcelUtil<WhActivity> util = new ExcelUtil<WhActivity>(WhActivity.class);
        return util.exportExcel(list, "activity");
    }

    /**
     * 新增活动
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存活动
     */
    @RequiresPermissions("whzj:activity:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WhActivity whActivity)
    {
        whActivity.setStatus("保存");
        return toAjax(whActivityService.insertWhActivity(whActivity));
    }

    /**
     * 修改活动
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        WhActivity whActivity = whActivityService.selectWhActivityById(id);
        mmap.put("whActivity", whActivity);
        return prefix + "/edit";
    }

    /**
     * 修改保存活动
     */
    @RequiresPermissions("whzj:activity:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WhActivity whActivity)
    {
        return toAjax(whActivityService.updateWhActivity(whActivity));
    }

    /**
     * 删除活动
     */
    @RequiresPermissions("whzj:activity:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        return toAjax(whActivityService.deleteWhActivityByIds(ids));
    }

    /**
     * 活动开始
     */
    @RequiresPermissions("whzj:activity:edit")
    @Log(title = "活动开始", businessType = BusinessType.UPDATE)
    @PostMapping("/activityBegin")
    @ResponseBody
    public AjaxResult activityBegin(String id)
    {
        WhActivity whActivity = new WhActivity();
        whActivity.setId(id);
        whActivity.setStatus("待开奖");
        return toAjax(whActivityService.updateWhActivity(whActivity));
    }

    /**
     * 随机开奖
     */
    @RequiresPermissions("whzj:activity:edit")
    @Log(title = "随机开奖", businessType = BusinessType.UPDATE)
    @PostMapping("/randomWin")
    @ResponseBody
    public AjaxResult randomWin(String id)
    {
        WhActivity whActivity = new WhActivity();
        whActivity.setId(id);
        whActivity.setStatus("已开奖");
        //获取活动参与次数
        WhActivityUser whActivityUser = new WhActivityUser();
        whActivityUser.setActivityId(id);
        List<WhActivityUser> whActivityUserList = whActivityUserService.selectWhActivityUserList(whActivityUser);
        Random random = new Random();
        int index = random.nextInt(whActivityService.selectWhActivityById(id).getAllPeople());
        whActivity.setUserId(whActivityUserList.get(index).getUserId());
        return toAjax(whActivityService.updateWhActivity(whActivity));
    }

    /**
     * 活动结束
     */
    @Transactional
    @RequiresPermissions("whzj:activity:edit")
    @Log(title = "活动结束", businessType = BusinessType.UPDATE)
    @PostMapping("/activityEnd")
    @ResponseBody
    public AjaxResult activityEnd(String id)
    {
        WhActivity whActivity = new WhActivity();
        whActivity.setId(id);
        whActivity.setStatus("已开奖");
        int num = whActivityService.updateWhActivity(whActivity);
        //设置活动参与者的状态，以及未中奖的状态
        WhActivity whActivity1 = whActivityService.selectWhActivityById(id);
        String userId = whActivity1.getUserId();
        WhActivityUser whActivityUser = new WhActivityUser();
        whActivityUser.setActivityId(id);
        List<WhActivityUser> whActivityUserList = whActivityUserService.selectWhActivityUserList(whActivityUser);
        for(WhActivityUser temp :whActivityUserList){
            if(userId.equals(temp.getUserId())){
                temp.setStatus("中奖");
            }else{
                temp.setStatus("未中奖");
            }
            whActivityUserService.updateWhActivityUser(temp);
        }
        //给中奖用户添加活动奖励积分
        WhIntegralHistory whIntegralHistory = new WhIntegralHistory();
        whIntegralHistory.setOperat("9");
        whIntegralHistory.setUserId(userId);
        whIntegralHistory.setIntegral(whActivity1.getAllIntegral());
        whIntegralHistory.setEventId(id);
        whIntegralHistoryService.insertWhIntegralHistory(whIntegralHistory);
        //给中奖用户添加积分余额
        WhUser whUser = new WhUser();
        whUser.setId(userId);
        whUser.setIntegral(whUserService.selectWhUserById(userId).getIntegral()+whActivity1.getIntegral());
        whUserService.updateWhUser(whUser);
        return toAjax(num);
    }

    /**
     * 设置中奖人
     */
    @GetMapping("/settingWin/{id}")
    public String settingWin(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("id", id);
        return prefix + "/activityuser";
    }

    /**
     * 设置中奖人
     */
    @RequiresPermissions("whzj:activity:edit")
    @Log(title = "设置中奖人", businessType = BusinessType.UPDATE)
    @PostMapping("/settingWin")
    @ResponseBody
    public AjaxResult settingWin(String id,String activityUserId)
    {
        //设置活动中间人id
        WhActivity whActivity = new WhActivity();
        whActivity.setId(id);
        whActivity.setUserId(whActivityUserService.selectWhActivityUserById(activityUserId).getUserId());
        return toAjax(whActivityService.updateWhActivity(whActivity));
    }
}
